﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Content.Pages;
using Marbles.Core.Connectors;
using System.Data;
using Marbles.Core.Settings;
using Marbles.Core.Content.Templates;

namespace Marbles.Web.Admin.Content
{
    public partial class TemplateMgr : ContentBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            cGrdItems.DataSource = new TemplateCollection(this.CurrentWeb, false);
            cGrdItems.DataBind();
        }

        private void Clear()
        {
            cTxtValue.Text = "";
            cTxtName.Text = "";
            cTxtName.Enabled = false;
            cBtnSave.Enabled = false;
            cBtnCancel.Enabled = false;
            cBtnDelete.Enabled = false;
            cBtnSave.CommandArgument = "";
            cBtnDelete.CommandArgument = "";
            cBtnNew.Enabled = true;
            cTxtValue.Enabled = false;
        }

        protected void Page_Command(object sender, CommandEventArgs e)
        {
            TemplateFile file = null;
            if (!string.IsNullOrEmpty(e.CommandArgument.ToString())) { file = new TemplateFile(this.CurrentWeb, e.CommandArgument.ToString(), true); }
            cTxtValue.Text = Server.UrlDecode(cTxtValue.Text);

            switch (e.CommandName)
            {
                case "New":
                    Clear();
                    cTxtName.Enabled = true;
                    cTxtValue.Enabled = true;
                    cBtnNew.Enabled = false;
                    cBtnSave.Enabled = true;
                    cBtnCancel.Enabled = true;
                    cBtnDelete.Enabled = false;
                    cTxtValue.Text = Template.DefaultTemplateValue;
                    cBtnSave.CommandName = "Insert";
                    break;
                case "Cancel":
                    Clear();
                    LoadData();
                    break;
                case "Modify":
                    cBtnNew.Enabled = false;
                    cBtnSave.Enabled = true;
                    cTxtValue.Enabled = true;
                    cBtnCancel.Enabled = true;
                    cBtnDelete.Enabled = true;
                    cBtnSave.CommandArgument = e.CommandArgument.ToString();
                    cBtnDelete.CommandArgument = e.CommandArgument.ToString();
                    cTxtName.Enabled = true;
                    cTxtName.Text = file.Name;
                    cTxtValue.Text = file.Value;
                    cBtnSave.CommandName = "Update";
                    break;
                case "Delete":
                    file.Delete();
                    Clear();
                    LoadData();
                    break;
                case "Update":                    
                    file.Value = cTxtValue.Text;
                    if (file.Name == cTxtName.Text)
                    {
                        file.Save();
                    }
                    else
                    {
                        file.SaveAs(cTxtName.Text);
                    }
                    
                    Clear();
                    LoadData();
                    break;
                case "Insert":
                    file = TemplateCollection.Create(this.CurrentWeb, cTxtName.Text);
                    file.Value = cTxtValue.Text;
                    file.Save();
                    Clear();
                    LoadData();
                    break;
            }
        }
    }
}